/**
 * Copyright (c) 2009.kingbi.
 * All rights reserved.
 */


package com.kingbi.system.roleFunction.jsfBean;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.component.UISelectBoolean;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.kingbi.system.roleFunction.model.RoleFunctionModel;
import com.kingbi.system.roleFunction.service.RoleFunctionService;
import com.kingbi.util.jsf.ListPageBean;


/**
 * Class description goes here.
 * 
 * Created by Auto Code Produce System
 * User:
 * Date: 20091030
 * Time: 170342
 */
public class RoleFunctionJsfBean extends RoleFunctionDataBean
{
        // 页面导航
        private String outcome;

        // 错误信息提示
        FacesMessage message;

        // //加载service
        RoleFunctionService roleFunctionService;

		static Logger logger = Logger.getLogger(RoleFunctionJsfBean.class);

		

         public void setRoleFunctionService(RoleFunctionService roleFunctionService)
        {
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance()
                    .getExternalContext().getRequest();
            String searchTag = (String) request.getParameter("searchTag");

            this.roleFunctionService = roleFunctionService;

            if(searchTag != null && searchTag.equals("isSearch"))
            {
                search();
            }
        }

        
        /**
         * 页面导航
         * 
         * @return
         */
        public String outcome()
        {
            logger.warn("outcomeoutcome");
            return outcome;
        }

        /**
         * 导向添加页面
         * 
         * @return
         */
        public String preAdd()
        {
             getEmptyJsfBean();
            logger.warn("**********************perAdd()");

            return "addRoleFunction";
        }
        
        /**
         * 导向修改页面
         * 
         * @return
         */
        public String preUpdate()
        {

            logger.warn("**********************perUpdate()");
            int n = tData.getRowCount();
            for (int i = 0; i < n; i++)
            {
                tData.setRowIndex(i);
                if(selected.isSelected())
                {
                    setChecked(false);
                    RoleFunctionModel tempModel = new RoleFunctionModel();
                    tempModel = (RoleFunctionModel) tData.getRowData();
                    RoleFunctionModel updateModel = new RoleFunctionModel();
//                        updateModel = roleFunctionService.loadRoleFunction(tempModel.getRoleFunID().getRoleCode());
                    getJsfDataBean(updateModel);
                }
            }
            logger.warn("********************************");
            return "updateRoleFunction";
        }
        
           
        /**
         * 提交添加或修改操作
         * 
         * @return
         */
        public String save()
        {
            logger.warn("**********************save()");
            RoleFunctionModel roleFunctionModel = new RoleFunctionModel();
            roleFunctionModel = super.getJsfModel();
            try
            {
                roleFunctionService.saveRoleFunction(roleFunctionModel);
                getEmptyJsfBean();
                search();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return "success";
        }

        /**
         * 删除
         * 
         * @return
         */
        public String delete()
        {
            try
            {
                int n = tData.getRowCount();
                for (int i = 0; i < n; i++)
                {
                    tData.setRowIndex(i);
                    if(selected.isSelected())
                    {
                        setChecked(false);
                        RoleFunctionModel tempModel = new RoleFunctionModel();
                        tempModel = (RoleFunctionModel) tData.getRowData();
                        roleFunctionService.deleteRoleFunction(3);
                        selected.setSelected(false);
                    }
                }            
                getEmptyJsfBean();
                search();
            }
            catch (Exception e)
            {
                return "failure";
            }
            return "success";
        }
        
        
     
        /**
         * 显示详细信息
         * 
         * @return
         */
        public String show()
        {
            try
            {
                FacesContext context = FacesContext.getCurrentInstance();
                String value = (String) context.getExternalContext().getRequestParameterMap().get(
                        "roleFunctionID");
                int roleFunID = 0 ;
                if(value!=null && !value.equals("") && !value.equals("null"))
                {
                    roleFunID = Integer.parseInt(value);
                }
                RoleFunctionModel updateModel = new RoleFunctionModel();
                updateModel = roleFunctionService.loadRoleFunction(roleFunID);
                getJsfDataBean(updateModel);
                return "showRoleFunction";

            }
            catch (Exception e)
            {
                return "failure";
            }

        }

        /**
         * 从其它页面返回到查询列表页面
         * @return
         */
        public String backToSearch()
        {
            getEmptyJsfBean();
            search();
            return "success";
        }

        /**
         * 查询方法
         * 
         * @return
         */
        public String search()
        {
            tData = null;
            selected = null;
            logger.warn("**********************search()");
            String s_new_CurrentPage = (String) FacesContext.getCurrentInstance().getExternalContext()
                    .getRequestParameterMap().get("newCurrentPage");
            String s_query_flag = (String) FacesContext.getCurrentInstance().getExternalContext()
                    .getRequestParameterMap().get("queryFlag");

            if(s_query_flag != null && s_query_flag.equals("ispage"))
            {
                listUpdateFlag = false;
            }
            else
            {
                listUpdateFlag = true;
            }

            RoleFunctionModel roleFunctionModel = new RoleFunctionModel();
            roleFunctionModel = super.getJsfModel();

            /*
             * 总记录数
             */
            if(listUpdateFlag)
            {
                setRecordCount(roleFunctionService.queryCount(roleFunctionModel));
            }
            /*
             * 当前页号
             */
            if(s_new_CurrentPage != null && !s_new_CurrentPage.equals("")
                    && !s_new_CurrentPage.equals("null"))
            {
                if(!(s_new_CurrentPage.equals("newPage")))
                {
                    setCurrentPage(Integer.parseInt(s_new_CurrentPage));
                }
            }
            else
            {
                setCurrentPage(1);
            }
            /*
             * 处理分页
             */
            dealPage(getRecordCount(), getPageRecord(), getCurrentPage());
            /*
             * 根据分页获取数据记录
             */
            try
            {
                setItems(roleFunctionService.listRoleFunction(roleFunctionModel, (getCurrentPage() - 1)
                        * getPageRecord() + 1, getPageRecord()));
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return "success";
        }

       public void validateRoleFunctionID(FacesContext context, UIComponent component, Object obj)
            throws ValidatorException
        {
            try
            {
                RoleFunctionModel updateModel = new RoleFunctionModel();
                updateModel = roleFunctionService.loadRoleFunction(Integer.parseInt(obj.toString()));
                if(updateModel != null )
                {
                    throw new ValidatorException(new FacesMessage("roleFunctionID", "ID重复--请重新填写标识"));
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

}

